<?php
namespace app\common\model;
class Finance extends Base{
    /**
     * 获取memo
     * @param  integer $type [description]
     * @param  string  $memo [description]
     * @return [type]        [description]
     */
    function memo($type=0,$_memo='',$getAll=false){
        $memo=[
            0=>'vip_buy', //VIP购买
            1=>[
                'name'=>'奖金领取',
                'children'=>[
                    'bonus_gain'=>'bonus_gain', //vip奖金领取
                    'week_bonus_gain'=>'week_bonus_gain', //周奖金领取
                ],
            ],
            2=>'agent_bonus', //代理商奖金
            3=>'withdraw_cash', //用户提现
            4=>'coin_buy', //虚拟币购买
            5=>[
                'name'=>'任务奖励',
                'children'=>[
                    'task_bonus_coin'=>'task_bonus_coin', //任务微豆奖励
                    'task_bonus_money'=>'task_bonus_money', //任务金额奖金
                ],
            ],
            // 100=>[
            //     'name'=>'',
            //     'children'=>[],
            // ],
        ];
        if($type<0){
            return $memo;
        }
        if(isset($memo[$type])){
            if(is_array($memo[$type])){
                if(!empty($memo[$type])&&isset($memo[$type]['children'][$_memo])){
                    return $memo[$type]['children'][$_memo];
                }elseif($getAll){
                    return $memo[$type];
                }
                return $memo[$type]['name'];
            }else{
                return $memo[$type];
            }
        }
        return $type;
    }
    /**
     * 存贮消费记录
     * @param  integer  $uid       所属用户
     * @param  integer  $from_id   来源用户
     * @param  double   $money     金额
     * @param  integer  $type      类型
     * @param  string   $remark    说明
     * @param  integer  $module_id 模块ID
     * @return [type]             [description]
     */
    function log($uid,$from_id,$money,$type,$options=[]){
        $data=array_merge([
            'uid'=>$uid,
            'from_id'=>$from_id,
            'money'=>$money,
            'type'=>$type,
            'module_id'=>0,
            'order_id'=>0,
            'remark'=>'',
            'memo'=>'',
            'now_time'=>strtotime(date('Y-m-d')),
            'y'=>date('Y'),
            'm'=>date('m'),
            'd'=>date('d'),
            's_time'=>time(),
        ],$options);
        if(empty($data['memo'])){
            $memo=$this->memo($type,$data['memo']);
            if(is_string($memo)){
                $data['memo']=$memo;
            }
        }
        return $this->add($data);
    }
    /**
     * 财务统计（用于后台首页财务统计列表显示数据）
     * @return [type] [description]
     */
    function statis($options=[]){
        $options=array_merge([
            'start'=>null,
            'end'=>time(),
            's'=>0,
            'isChart'=>true,
            'order'=>'',
        ],$options);
        extract($options);
        if(is_null($start)){
            $start=strtotime(date('Y-m'));
        }
        $e=config('paginate.list_rows')+$s;
        $table=$this->getTable();
        
        $field='now_time'; //期号
        $field.=',sum(if(`type`=0,`money`,0)) as `vip_buy`'; //VIP购买
        //vip奖金领取
        $field.=',sum(if(`type`=1 and `memo`="bonus_gain",`money`,0)) as `bonus_gain`'; 
        $field.=',sum(if(`type`=1 and `memo`="week_bonus gain",`money`,0)) as `week_bonus_gain`'; //周奖金领取
        $field.=',sum(if(`type`=2,`money`,0)) as `agent_bonus`'; //代理商奖金
        $field.=',sum(if(`type`=3,money,"0")) as `withdraw_cash`'; //用户提现
        $field.=',sum(if(`type`=4,money,"0")) as `coin_buy`'; //虚拟币购买

        $field.=',sum(if(`type`=5 and `memo`="task_bonus_coin",`money`,0)) as `bonus_coin`'; //任务虚拟币奖励
        $field.=',sum(if(`type`=5 and `memo`="task_bonus_money",`money`,0)) as `bonus_money`'; //任务金额奖励
        
        $sql="select $field from $table where `now_time` >= $start and `now_time` < $end group by now_time {$order} limit {$s},{$e}";
        $list=$this->query($sql);
        if(!$isChart){
            $allSql="select $field from $table where `now_time` >= $start and `now_time` < $end";
            $all=$this->query($allSql);
            return [$list,$all[0]];
        }
        $res=[
            'x'=>['x'], //期号
            'vip_buy'=>['vip_buy'], //VIP购买
            'bonus_gain'=>['bonus_gain'], //vip奖金领取
            'week_bonus_gain'=>['week_bonus_gain'], //周奖金领取
            'agent_bonus'=>['agent_bonus'], //代理商奖金
            'withdraw_cash'=>['withdraw_cash'], //用户提现
            'coin_buy'=>['coin_buy'], //虚拟币购买
            
            'bonus_coin'=>['bonus_coin'], //任务虚拟币奖励
            'bonus_money'=>['bonus_money'], //任务金额奖励

            'income'=>['income'], //收入
            'expenditure'=>['expenditure'], //支出
            'profit'=>['profit'], //收益
        ];
        foreach ($res as $k => $v) {
            if($k!=='x'){
                $res['title'][$k]=lang($k);
            }
        }
        foreach ($list as $v) {
            $res['x'][]=$v['now_time'];
            $res['vip_buy'][]=$v['vip_buy'];
            $res['bonus_gain'][]=$v['bonus_gain'];
            $res['week_bonus_gain'][]=$v['week_bonus_gain'];
            $res['agent_bonus'][]=$v['agent_bonus'];
            $res['withdraw_cash'][]=$v['withdraw_cash'];
            $res['coin_buy'][]=$v['coin_buy'];

            $res['bonus_coin'][]=$v['bonus_coin'];
            $res['bonus_money'][]=$v['bonus_money'];
            
            $res['income'][]=$v['vip_buy']+$v['coin_buy'];
            
            $res['expenditure'][]=$v['withdraw_cash'];
            $res['profit'][]=($v['vip_buy']+$v['coin_buy'])-$v['withdraw_cash'];
        }

        return $res;
    }


    function user(){
        return $this->belongsTo('member','uid')->field('id,name')->bind(['user'=>'name']);
    }
    function from(){
        return $this->belongsTo('member','from_id')->field('id,name')->bind(['from'=>'name']);
    }
    function order(){
        return $this->belongsTo('order','order_id')->field('id,number')->bind(['order'=>'number']);
    }
}